package com.its.net.udp.server.config;

import com.its.common.net.server.impl.UdpServer;
import com.its.net.udp.server.handler.CustomHandler;
import io.netty.channel.SimpleChannelInboundHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Xiaxiaopeng
 * @description 功能描述
 * @create 2023/5/31 16:02
 */
@Configuration
@ConditionalOnProperty(prefix = "its.net.udp.server", name = "enabled", havingValue = "true")
public class UdpServerConfig {
    final CustomHandler udpServerHandler;

    public UdpServerConfig(CustomHandler udpServerHandler) {
        this.udpServerHandler = udpServerHandler;
    }

    @Bean
    @ConditionalOnBean(UdpServerConfig.class)
    public UdpServer customUdpServer() {
        //1. 处理器组
        List<SimpleChannelInboundHandler> handlers = new ArrayList<>();
        handlers.add(udpServerHandler);

        //2. 构建服务器
        UdpServer server = new UdpServer(10888, handlers, 10, TimeUnit.MINUTES);

        server.start();

        return server;
    }
}
